package com.gwclh.personnel.common.exception;

import com.gwclh.personnel.bean.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.NonTransientDataAccessException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;
import java.io.IOException;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    //    用户登录认证不通过异常

    @ExceptionHandler(ShiroException.class)
    public ResultVo handle401(ShiroException e){
        log.error("认证异常:-------->{}",e.getMessage());
        return ResultVo.unauthorized();
    }
    /**
     * 用户未认证异常
     * @param e
     * @return
     */
    @ExceptionHandler(AuthenticationException.class)
    public ResultVo handle401(AuthenticationException e){
        log.error("认证异常:-------->{}",e);
        return ResultVo.unauthorized();
    }


    @ExceptionHandler(value = UnauthorizedException.class)
    public ResultVo handler(UnauthorizedException e)  {
        log.error("无权限:-------------->");
        return ResultVo.fail(401,"您没有权限");
    }

    /**
     * 请求方法错误
     * @param e
     * @return
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public ResultVo handlerMethod(HttpRequestMethodNotSupportedException e)  {
        log.error("请求方法错误:-------------->");
        return ResultVo.fail(400,"请求方法错误");
    }

    /**
     * 传递参数错误异常
     * @return
     */
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public ResultVo NotReadableException(HttpMessageNotReadableException e){
        log.error("参数错误",e);
        return ResultVo.fail(400,"请传递JSON数据");
    }

    @ExceptionHandler(value = NumberFormatException.class)
    public ResultVo NumberFormatException(NumberFormatException e){
        log.error(e.getMessage());
        return ResultVo.fail(400,"参数类型错误");
    }


    /**
     * 处理Assert的异常
     */
    @ExceptionHandler(value = IllegalArgumentException.class)
    public ResultVo handler(IllegalArgumentException e) throws IOException {
        log.error("Assert异常:-------------->{}",e.getMessage());
        return ResultVo.fail(e.getMessage());
    }


    /**
     * @Validated 校验错误异常处理
     */
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResultVo handler(MethodArgumentNotValidException e) throws IOException {
        log.error("参数校验错误异常1:-------------->",e);
        BindingResult bindingResult = e.getBindingResult();
        ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();

        return ResultVo.fail(405,objectError.getDefaultMessage());
    }

    @ExceptionHandler(value = ConstraintViolationException.class)
    public ResultVo handler(ConstraintViolationException e) throws IOException{
        log.error("参数校验错误异常2:--------->",e);
        return ResultVo.fail(405,e.getConstraintViolations().stream().findFirst().get().getMessage());
    }

    @ExceptionHandler(value = NonTransientDataAccessException.class)
    public ResultVo handler(NonTransientDataAccessException e){
        log.error("数据库错误:--------->",e);
        return ResultVo.error();
    }




    @ExceptionHandler(value = RuntimeException.class)
    public ResultVo handler(RuntimeException e) throws IOException {
        log.error("运行时异常:-------------->",e);
        return ResultVo.error();
    }


}
